//
//  MainTabBarViewController.swift
//  sinaWeibo
//
//  Created by 张国梁 on 16/3/26.
//  Copyright © 2016年 CoderGL. All rights reserved.
//

import UIKit

class MainTabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        // 加号按钮
        tabBar.addSubview(composeBtn)
        let width = UIScreen.mainScreen().bounds.size.width / CGFloat(childViewControllers.count)
        composeBtn.frame = CGRectMake(0, 0, width, tabBar.bounds.size.height)
        composeBtn.frame = CGRectOffset(composeBtn.frame, 2 * width, 0)
    }
    
    
    // MARK : 添加所有子控制器
    private func addChildViewControllers() {
        
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
        let jsonData = NSData(contentsOfFile: path!)
        if let data = jsonData {
            
            do {
                let dataArr = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)
                let arr = dataArr as! [[String: String]]
                for var dict in arr {
                    addChildViewController(dict["vcName"]!, imageName: dict["imageName"]!, title: dict["title"]!)
                }
            } catch {
                addChildViewController("HomeTableViewController", imageName: "tabbar_home", title: "首页")
                addChildViewController("MessageTableViewController", imageName: "tabbar_message_center", title: "消息")
                addChildViewController("DiscoverTableViewController", imageName: "tabbar_discover", title: "发现")
                addChildViewController("ProfileTableViewController", imageName: "tabbar_profile", title: "我")
            }
        }
    }
    
    private func addChildViewController(childControllerName: String, imageName: String, title: String) {
        
        // 获得项目名称－－－>命名空间名字
        let namespace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        // 拼接字符串并转换为类
        let cls: AnyClass? = NSClassFromString(namespace + "." + childControllerName)
        // 转换为UIViewController类型
        let vcCls = cls as! UIViewController.Type
        let vc = vcCls.init() // 创建
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        let nav = MainNavgationController(rootViewController: vc)
        addChildViewController(nav)
    }

    func comopseBtnClick() -> Void {
        print(#function)
    }
    
    private lazy var composeBtn: UIButton = {
        let composeBtn = UIButton()
        composeBtn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        composeBtn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        composeBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        composeBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        composeBtn.addTarget(self, action: #selector(MainTabBarViewController.comopseBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        return composeBtn
    }()
}
